<?php
declare (strict_types = 1);

namespace app\home\controller;

use app\common\controller\Frontend;
use app\common\controller\FrontUserBase;
use think\facade\Db;
use think\facade\View;
use app\home\model\User as UserModel;

class User extends Frontend
{
    public function initialize()
    {
        parent::initialize();
    }


    /**
     * 用户登陆
     */
    public function login() {
        if (request()->isPost()) {
            $username = request()->param('username');
            $password = request()->param('password');
            $remember = request()->param('remember');
            if (empty($username)) {
                return fail(0,'请输入账号！','');
            }
            if (empty($password)) {
                return fail(0,'请输入密码！','');
            }
            $uid = Db::name('user')->where(array('username'=>$username,'status'=>1, 'password'=>sha1($password)))->value('id');
            if (!$uid) {
                return fail(0,'账号不存在或密码错误！','');

            }

//            //登陆
            $this->visitor->login($uid, $remember);
            if (request()->isAjax()) {
                return success(1,'登陆成功！','');
            } else {
                //跳转到登陆前页面（执行同步操作）
                $ret_url = request()->param('ret_url');
                $refer = request()->param('refer');
                if ($refer) {
                    $refer = base64_decode($refer);
                    $ret_url = $refer;
                }
                return success(1,'登陆成功！',$ret_url);
            }
        } else {
            return view();
        }
    }

    /**
     * 用户退出
     */
    public function logout() {
        $this->visitor->logout();
        return redirect('/home');
    }

    /**
     * 用户注册
     */
    public function register()
    {
        if (request()->isPost()) {
            //方式
            $iscaptcha = request()->param('iscaptcha');
            if ($iscaptcha) {
                //验证
                $captcha = request()->param('captcha');
                //验证码
                if (!captcha_check($captcha)) {
                    return fail(0,'验证码错误','','');
                }
            }
            $username = request()->param('username');
            $email = request()->param('email');
            $map['email'] = $email;
            $reemail = $this->_mod->where($map)->find();
            if ($reemail) {
                return fail(0,'该邮箱已经注册','','');
            }
            $password = request()->param('password');
            if ($password == '') {
                return fail(0,'密码不能为空','','');
            }
            $repassword = request()->param('repassword');
            if ($password != $repassword) {
                return fail(0,'两次密码不一致','','');
            }
            //注册
            $add_data = [
                'username' => $username,
                'password' => sha1($password),
                'email' => $email,
                'create_time'=>time()
            ];

            $uid = $this->_mod->create($add_data)->getData('id');
            if ($uid) {
                $this->visitor->login($uid);
                return success(1,'注册成功','','');
            } else {
                return fail(0,'注册失败','','');
            }
        } else {
            //关闭注册
            return view();
        }
    }

}


